<template>
  <div class="temp-container">
    <a-modal
      :visible="visible"
      title="生成退租协议"
      centered
      :destroyOnClose="true"
      @ok="toDownload"
      @cancel="closeModal"
      okText="确定并下载"
    >
      <a-form layout="vertical">
        <a-form-item label="退租协议打印模板" v-bind="validateInfos.templateId">
          <a-select
            v-model:value="modelRef.templateId"
            :options="
              list.map((item) => ({
                value: item.id,
                label: item.template_name,
              }))
            "
          >
          </a-select>
        </a-form-item>
      </a-form>
      <template #footer>
        <a-button @click="closeModal"> 取消 </a-button>
        <a-button type="primary" @click="toDownload" :loading="loading">
          确定并下载
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import { defineComponent, reactive, ref, watch } from "vue";
import { Form } from "ant-design-vue";
const useForm = Form.useForm;

export default defineComponent({
  name: "AddBill",
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    list: {
      type: Array,
      default: () => [],
    },
    loading: {
      type: Boolean,
      default: false,
    },
  },
  setup(props, context) {
    const list = ref(Number(props.list));
    console.log("props.list", props.list);

    const modelRef = reactive({
      templateId: "",
    });

    const rulesRef = reactive({
      templateId: [{ required: true, message: "请选择打印模板" }],
    });

    const { validate, validateInfos } = useForm(modelRef, rulesRef);

    const closeModal = () => {
      context.emit("close");
    };

    const toDownload = () => {
      validate().then(() => {
        context.emit("success", modelRef.templateId);
      });
    };

    const init = () => {};

    watch(
      () => props.visible,
      (val) => {
        if (val) {
          // 打开时初始化数据
          init();
        } else {
          // 关闭时重置数据
        }
      }
    );

    return {
      closeModal,
      toDownload,
      validateInfos,
      modelRef,
    };
  },
});
</script>

<style lang="less" scoped></style>
